import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import qiankun from 'vite-plugin-qiankun'; //微服务框架 - 当为子应用时开启打包配置

import path from 'path';
import { themePreprocessorPlugin } from '@zougt/vite-plugin-theme-preprocessor';
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
import unoCSS from 'unocss/vite';

// https://cn.vitejs.dev/guide/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());

  return {
    plugins: [
      vue(),
      qiankun('dms-web', {
        useDevMode: true
      }),
      unoCSS()
      // themePreprocessorPlugin({
      //   less: {
      //     // 各个主题文件的位置
      //     multipleScopeVars: [
      //       {
      //         scopeName: 'theme-default',
      //         path: path.resolve('src/theme/default.less')
      //       },
      //       {
      //         scopeName: 'theme-green',
      //         path: path.resolve('src/theme/green.less')
      //       },
      //       {
      //         scopeName: 'theme-dark',
      //         path: path.resolve('src/theme/dark.less')
      //       }
      //     ],
      //   },
      // }),
    ],
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          modifyVars: {
            'primary-color': '#005aea'
            // 'link-color': '#1DA57A',
            // 'border-radius-base': '2px',
          },
          javascriptEnabled: true
        }
      }
    },
    base: `${qiankunWindow.__POWERED_BY_QIANKUN__ ? '/pdm-web/dms-web/' : '/dms-web/'}`,
    envDir: './environment', //环境变量文件
    // envPrefix: 'VITE_', //环境变量抬头
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      },
      extensions: ['.js', '.json', '.ts'] // 使用路径别名时想要省略的后缀名，可以自己 增减
    },
    server: {
      host: '0.0.0.0', //ip地址
      port: 10400, //端口号
      open: true, //启动后是否自动打开浏览器
      proxy: {
        '/system': {
          target: 'https://wpis-dev.vpclub.cn',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/system/, '')
        }
      }
    },
    build: {
      rollupOptions: {
        output: {
          manualChunks: (id: any) => {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          }
        }
      }
    }
  };
});
